package ejb.facades;

import entities.Login;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Stateless
public class LoginFacade extends AbstractFacade<Login> implements LoginFacadeLocal {
    @PersistenceContext(unitName = "Trabalho_PDPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    @Override
    public Login findByUserName(String userName) {
        TypedQuery<Login> createNamedQuery = em.createNamedQuery("Login.findByUsername", Login.class);
        createNamedQuery.setParameter("username", userName);
        List<Login> resultList = createNamedQuery.getResultList();
        if(resultList.isEmpty()) {
            return null;
        } else {
            return resultList.get(0);
        }
    }

    public LoginFacade() {
        super(Login.class);
    }

}
